package com.liyuan.yiqixing.config;

import com.liyuan.yiqixing.component.security.AuthenticationSuccessHandler;
import com.liyuan.yiqixing.component.security.DisableUrlSessionIdFilter;
import com.liyuan.yiqixing.component.security.JwtAuthenticationTokenFilter;
import com.liyuan.yiqixing.component.security.SecurityUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

@Configurable
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Autowired
    @Qualifier("securityUserDetailsService")
    UserDetailsService securityUserDeatilsService;

    @Autowired
    JwtAuthenticationTokenFilter authenticationTokenFilter;

    @Autowired
    LogoutSuccessHandler logoutSuccessHandler;

    @Autowired
    AccessDeniedHandler accessDeniedHandler;

    @Autowired
    AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    DisableUrlSessionIdFilter disableUrlSessionIdFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        super.configure(http);
        http.csrf().disable();
        http.headers().frameOptions().disable();
        http.authorizeRequests().antMatchers("/publish","/personal.html","/personal","/replies","/replies.html","/publish","/publish.html").hasAuthority("/personal");
        http.authorizeRequests().antMatchers(HttpMethod.POST,"/item/signup").hasAuthority("/signup");
        http.authorizeRequests().antMatchers("/recruit","/recruit.html","/publishRecruitment").hasAuthority("/recruit");
        http.formLogin().loginPage("/login").successHandler(authenticationSuccessHandler);

        http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
        http.userDetailsService(securityUserDeatilsService);
        http.logout().logoutSuccessUrl("/").logoutSuccessHandler(logoutSuccessHandler);
        //关闭session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //关闭缓存
        http.headers().cacheControl();
        http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        http.addFilterBefore(disableUrlSessionIdFilter,UsernamePasswordAuthenticationFilter.class);

        http.authorizeRequests().anyRequest().permitAll();
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**","/js/**","/webjars/**","/images/**","/fonts/**");
    }
}
